package models

import (
	"errors"
	"gorm.io/gorm"
	"tuowei.com/common/models"
)

type DeptArr []SysDept

func (d DeptArr) Len() int {
	return len(d)
}
func (d DeptArr) Less(i, j int) bool {
	return d[i].Sort < d[j].Sort
}
func (d DeptArr) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}

type SysDept struct {
	DeptId   int    `json:"deptId" gorm:"primaryKey;autoIncrement;"` //部门编码
	ParentId int    `json:"parentId" gorm:""`                        //上级部门
	DeptPath string `json:"deptPath" gorm:"size:255;"`               //
	DeptName string `json:"deptName"  gorm:"size:128;"`              //部门名称
	Sort     int    `json:"sort" gorm:"size:4;"`                     //排序
	UserId   int    `json:"user_id" gorm:"size:128;"`                //负责人
	Phone    string `json:"phone" gorm:"size:11;"`                   //手机
	Email    string `json:"email" gorm:"size:64;"`                   //邮箱
	Status   int    `json:"status" gorm:"size:4;"`                   //状态
	models.ControlBy
	models.ModelTime
	DataScope string    `json:"dataScope" gorm:"-"`
	Params    string    `json:"params" gorm:"-"`
	Children  []SysDept `json:"children" gorm:"-"`
}

type LecturerDept struct {
	DeptId   int    `json:"dept_id" gorm:"primaryKey;autoIncrement"` //部门编码
	DeptName string `json:"dept_name"`
}

func (LecturerDept) TableName() string {
	return "sys_dept"
}

type ClassDept struct {
	DeptId   int    `json:"dept_id" gorm:"primaryKey;autoIncrement"` //部门编码
	DeptName string `json:"dept_name"`
}

func (ClassDept) TableName() string {
	return "sys_dept"
}

type CollegesDept struct {
	DeptId   int    `json:"dept_id" gorm:"primaryKey;autoIncrement;"` //部门编码
	DeptName string `json:"dept_name"`                                //部门名称
}

func (CollegesDept) TableName() string {
	return "sys_dept"
}

type StudentDept struct {
	DeptId   int    `json:"dept_id" gorm:"primaryKey;autoIncrement"` //部门编码
	DeptName string `json:"dept_name"  gorm:"size:128;"`             //部门名称
}

type CityDept struct {
	DeptId   int    `json:"dept_id"`    //部门编码
	DeptName string `json:"dept_name" ` //部门名称
}

func (CityDept) TableName() string {
	return "sys_dept"
}

type DistrictDept struct {
	DeptId   int    `json:"dept_id"`    //部门编码
	DeptName string `json:"dept_name" ` //部门名称
}

func (DistrictDept) TableName() string {
	return "sys_dept"
}

type AreaDept struct {
	DeptId   int    `json:"dept_id"`    //部门编码
	DeptName string `json:"dept_name" ` //部门名称
}

type TeachingPoint struct {
	Id      int                 `json:"id"`    //教学点id
	Name    string              `json:"name" ` //教学点名字
	Address string              `json:"address"`
	Classes []*TeacherClassList `gorm:"foreignKey:dept_id;references:id"`
}

func (tp TeachingPoint) TableName() string {
	return "sys_teaching_point"
}

func (AreaDept) TableName() string {
	return "sys_dept"
}

func (StudentDept) TableName() string {
	return "sys_dept"
}

func (SysDept) TableName() string {
	return "sys_dept"
}

func (e *SysDept) Generate() models.ActiveRecord {
	o := *e
	return &o
}

func (e *SysDept) GetId() interface{} {
	return e.DeptId
}

func (e *SysDept) GetListsByIds(db *gorm.DB, ids []int) (map[int]SysDept, error) {
	if db == nil {
		return nil, errors.New("get db connect failed")
	}
	lists := make([]SysDept, 0)
	arr := make([]int, 0)
	for k := range ids {
		if ids[k] != 0 {
			arr = append(arr, ids[k])
		}
	}
	err := db.Debug().Table(SysDept{}.TableName()).Where("dept_id in ?", arr).Find(&lists).Error
	m := make(map[int]SysDept)
	if err != nil {
		return m, err
	}
	for k := range lists {
		m[lists[k].DeptId] = lists[k]
	}
	return m, nil
}
